﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace dzs.DAL
{
	/// <summary>
	/// 数据访问类:monthsked
	/// </summary>
	public partial class monthsked
	{
		public monthsked()
		{}

        public bool Updateuid(string qidlist, int uid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update monthsked set  uid=" + uid);
            strSql.Append(" where mid in (" + qidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("mid", "monthsked"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int mid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from monthsked");
			strSql.Append(" where mid=@mid");
			SqlParameter[] parameters = {
					new SqlParameter("@mid", SqlDbType.Int,4)
			};
			parameters[0].Value = mid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(dzs.Model.monthsked model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into monthsked(");
			strSql.Append("spotunit,spottype,spotnum,spotcontent,spotopinion,plandate,uid,qresult,question,monthsupplyid)");
			strSql.Append(" values (");
			strSql.Append("@spotunit,@spottype,@spotnum,@spotcontent,@spotopinion,@plandate,@uid,@qresult,@question,@monthsupplyid)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@spotunit", SqlDbType.NVarChar,30),
					new SqlParameter("@spottype", SqlDbType.NVarChar,10),
					new SqlParameter("@spotnum", SqlDbType.Float,8),
					new SqlParameter("@spotcontent", SqlDbType.NVarChar,200),
					new SqlParameter("@spotopinion", SqlDbType.NVarChar,50),
					new SqlParameter("@plandate", SqlDbType.DateTime),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@qresult", SqlDbType.NVarChar,10),
					new SqlParameter("@question", SqlDbType.NVarChar,300),
					new SqlParameter("@monthsupplyid", SqlDbType.Int,4)};
			parameters[0].Value = model.spotunit;
			parameters[1].Value = model.spottype;
			parameters[2].Value = model.spotnum;
			parameters[3].Value = model.spotcontent;
			parameters[4].Value = model.spotopinion;
			parameters[5].Value = model.plandate;
			parameters[6].Value = model.uid;
			parameters[7].Value = model.qresult;
			parameters[8].Value = model.question;
			parameters[9].Value = model.monthsupplyid;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
        public bool Updateresult(dzs.Model.monthsked model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update monthsked set ");           
            strSql.Append("spotopinion=@spotopinion,");           
            strSql.Append("qresult=@qresult,");
            strSql.Append("question=@question ");            
            strSql.Append(" where mid=@mid");
            SqlParameter[] parameters = {					
					new SqlParameter("@spotopinion", SqlDbType.NVarChar,50),					
					new SqlParameter("@qresult", SqlDbType.NVarChar,10),
					new SqlParameter("@question", SqlDbType.NVarChar,300),					
					new SqlParameter("@mid", SqlDbType.Int,4)};
            parameters[0].Value = model.spotopinion;
            parameters[1].Value = model.qresult;
            parameters[2].Value = model.question;
            parameters[3].Value = model.mid;          

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(dzs.Model.monthsked model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update monthsked set ");
			strSql.Append("spotunit=@spotunit,");
			strSql.Append("spottype=@spottype,");
			strSql.Append("spotnum=@spotnum,");
			strSql.Append("spotcontent=@spotcontent,");
			strSql.Append("spotopinion=@spotopinion,");
			strSql.Append("plandate=@plandate,");
			strSql.Append("uid=@uid,");
			strSql.Append("qresult=@qresult,");
			strSql.Append("question=@question,");
			strSql.Append("monthsupplyid=@monthsupplyid");
			strSql.Append(" where mid=@mid");
			SqlParameter[] parameters = {
					new SqlParameter("@spotunit", SqlDbType.NVarChar,30),
					new SqlParameter("@spottype", SqlDbType.NVarChar,10),
					new SqlParameter("@spotnum", SqlDbType.Float,8),
					new SqlParameter("@spotcontent", SqlDbType.NVarChar,200),
					new SqlParameter("@spotopinion", SqlDbType.NVarChar,50),
					new SqlParameter("@plandate", SqlDbType.DateTime),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@qresult", SqlDbType.NVarChar,10),
					new SqlParameter("@question", SqlDbType.NVarChar,300),
					new SqlParameter("@monthsupplyid", SqlDbType.Int,4),
					new SqlParameter("@mid", SqlDbType.Int,4)};
			parameters[0].Value = model.spotunit;
			parameters[1].Value = model.spottype;
			parameters[2].Value = model.spotnum;
			parameters[3].Value = model.spotcontent;
			parameters[4].Value = model.spotopinion;
			parameters[5].Value = model.plandate;
			parameters[6].Value = model.uid;
			parameters[7].Value = model.qresult;
			parameters[8].Value = model.question;
			parameters[9].Value = model.monthsupplyid;
			parameters[10].Value = model.mid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int mid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from monthsked ");
			strSql.Append(" where mid=@mid");
			SqlParameter[] parameters = {
					new SqlParameter("@mid", SqlDbType.Int,4)
			};
			parameters[0].Value = mid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string midlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from monthsked ");
			strSql.Append(" where mid in ("+midlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public dzs.Model.monthsked GetModel(int mid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 mid,spotunit,spottype,spotnum,spotcontent,spotopinion,plandate,uid,qresult,question,monthsupplyid from monthsked ");
			strSql.Append(" where mid=@mid");
			SqlParameter[] parameters = {
					new SqlParameter("@mid", SqlDbType.Int,4)
			};
			parameters[0].Value = mid;

			dzs.Model.monthsked model=new dzs.Model.monthsked();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public dzs.Model.monthsked DataRowToModel(DataRow row)
		{
			dzs.Model.monthsked model=new dzs.Model.monthsked();
			if (row != null)
			{
				if(row["mid"]!=null && row["mid"].ToString()!="")
				{
					model.mid=int.Parse(row["mid"].ToString());
				}
				if(row["spotunit"]!=null)
				{
					model.spotunit=row["spotunit"].ToString();
				}
				if(row["spottype"]!=null)
				{
					model.spottype=row["spottype"].ToString();
				}
				if(row["spotnum"]!=null && row["spotnum"].ToString()!="")
				{
					model.spotnum=decimal.Parse(row["spotnum"].ToString());
				}
				if(row["spotcontent"]!=null)
				{
					model.spotcontent=row["spotcontent"].ToString();
				}
				if(row["spotopinion"]!=null)
				{
					model.spotopinion=row["spotopinion"].ToString();
				}
				if(row["plandate"]!=null && row["plandate"].ToString()!="")
				{
					model.plandate=DateTime.Parse(row["plandate"].ToString());
				}
				if(row["uid"]!=null && row["uid"].ToString()!="")
				{
					model.uid=int.Parse(row["uid"].ToString());
				}
				if(row["qresult"]!=null)
				{
					model.qresult=row["qresult"].ToString();
				}
				if(row["question"]!=null)
				{
					model.question=row["question"].ToString();
				}
				if(row["monthsupplyid"]!=null && row["monthsupplyid"].ToString()!="")
				{
					model.monthsupplyid=int.Parse(row["monthsupplyid"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("SELECT [projectName],[sname],[material],[unit],[winBid],[num],[mbm],[usework],[mid],[spotunit],[spottype],[spotnum],[spotcontent],[spotopinion],[plandate],[uid],[qresult],[question],[user_name]  FROM  [Vmonthsked] ");
			strSql.Append(" where 1=1 ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" mid,spotunit,spottype,spotnum,spotcontent,spotopinion,plandate,uid,qresult,question,monthsupplyid ");
			strSql.Append(" FROM monthsked ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM monthsked ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.mid desc");
			}
			strSql.Append(")AS Row, T.*  from monthsked T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "monthsked";
			parameters[1].Value = "mid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

